package Weather;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ValueChangeEvent;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.List;

/**
 * Created by Новак on 26.11.2014.
 */
@ManagedBean (name = "weather")
@SessionScoped
public class Weather {
    private String xml;
    private HttpRequests httpRequests;
    private File fileCities =new File("/home/vanches/Java/XML_File/Cities.xml");
    private List<Cities.Country> countriesList;
    private List<Cities.Country.City> citiesList;
    private String countriesUser;
    private String citiesUser;
    private Forecast forecast;
    Cities cities;

    @PostConstruct
    public void init(){
        //Загрузка списка стран
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(Cities.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            cities = (Cities) jaxbUnmarshaller.unmarshal(fileCities);
            countriesList=cities.getCountry();
        }

        catch (JAXBException e) {
            e.printStackTrace();
        }
        httpRequests=new HttpRequests();
    }

    public void countrySet(ValueChangeEvent e){

        countriesUser=e.getNewValue().toString();
        for(int i=0; i<countriesList.size();i++){
            if(countriesList.get(i).getName().equals(countriesUser)){
               citiesList=countriesList.get(i).city;
                break;
            }
        }
    }
    public void CitiesSet(ValueChangeEvent e){

        downolader(e);


    }

    private void downolader(ValueChangeEvent e) {
        citiesUser=e.getNewValue().toString();

        //Посылка GET запроса сайта, и запись ответа в xml файл
        try {
            xml=httpRequests.sendGet(citiesUser,"fff");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        InputStream pStm = new ByteArrayInputStream(xml.getBytes());
        JAXBContext jaxbContext = null;
        try {
            jaxbContext = JAXBContext.newInstance(Forecast.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            forecast = (Forecast) jaxbUnmarshaller.unmarshal(pStm);
        } catch (JAXBException e1) {
            e1.printStackTrace();
        }
    }


    public List<Cities.Country> getCountriesList() {
        return countriesList;
    }

    public void setCountriesList(List<Cities.Country> countriesList) {
        this.countriesList = countriesList;
    }

    public List<Cities.Country.City> getCitiesList() {
        return citiesList;
    }

    public void setCitiesList(List<Cities.Country.City> citiesList) {
        this.citiesList = citiesList;
    }

    public String getCountriesUser() {
        return countriesUser;
    }

    public void setCountriesUser(String countriesUser) {
        this.countriesUser = countriesUser;
    }

    public String getCitiesUser() {
        return citiesUser;
    }

    public void setCitiesUser(String citiesUser) {
        this.citiesUser = citiesUser;
    }

    public Forecast getForecast() {
        return forecast;
    }

    public void setForecast(Forecast forecast) {
        this.forecast = forecast;
    }

    public Cities getCities() {
        return cities;
    }

    public void setCities(Cities cities) {
        this.cities = cities;
    }
}
